﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SplashScreenThreaded1 {
    public partial class SplashScreenForm : Form {
        delegate void StringParameterDelegate(string Text);
        delegate void StringParameterWithStatusDelegate(string Text, TypeOfMessage tom);
        delegate void SplashShowCloseDelegate();

        /// <summary>
        /// To ensure splash screen is closed using the API and not by keyboard or any other things
        /// </summary>
        bool CloseSplashScreenFlag = false;

        /// <summary>
        /// Base constructor
        /// </summary>
        public SplashScreenForm() {
            InitializeComponent();
            this.label1.Text = "";
            this.label1.Parent = this.pictureBox1;
            this.label1.BackColor = Color.Transparent;
            label1.ForeColor = Color.Green;

            this.label2.Text = "";
            this.label2.Parent = this.pictureBox1;
            this.label2.BackColor = Color.Transparent;
            label2.ForeColor = Color.Green;

            this.label3.Text = MainForm.SplashTitle;
            this.label3.Parent = this.pictureBox1;
            this.label3.BackColor = Color.Transparent;
            label3.ForeColor = Color.Green;

            //this.progressBar1.Parent = this.pictureBox1;
            //this.progressBar1.BackColor = Color.Transparent;

            progressBar1.Show();
        }

        /// <summary>
        /// Base constructor
        /// </summary>
        [Obsolete("Doesn't work like it should.")]
        public SplashScreenForm(string title) {
            InitializeComponent();
            this.label1.Text = "";
            this.label1.Parent = this.pictureBox1;
            this.label1.BackColor = Color.Transparent;
            label1.ForeColor = Color.Green;

            this.label2.Text = "";
            this.label2.Parent = this.pictureBox1;
            this.label2.BackColor = Color.Transparent;
            label2.ForeColor = Color.Green;

            this.label3.Text = title;
            this.label3.Parent = this.pictureBox1;
            this.label3.BackColor = Color.Transparent;
            label3.ForeColor = Color.Black;

            //this.progressBar1.Parent = this.pictureBox1;
            //this.progressBar1.BackColor = Color.Transparent;

            progressBar1.Show();
        }

        /// <summary>
        /// Displays the splashscreen
        /// </summary>
        public void ShowSplashScreen() {
            if (InvokeRequired) {
                // We're not in the UI thread, so we need to call BeginInvoke
                BeginInvoke(new SplashShowCloseDelegate(ShowSplashScreen));
                return;
            }
            this.Show();
            Application.Run(this);
        }

        /// <summary>
        /// Closes the SplashScreen
        /// </summary>
        public void CloseSplashScreen() {
            if (InvokeRequired) {
                // We're not in the UI thread, so we need to call BeginInvoke
                BeginInvoke(new SplashShowCloseDelegate(CloseSplashScreen));
                return;
            }
            CloseSplashScreenFlag = true;
            this.Close();
        }

        /// <summary>
        /// Update text in default green color of success message
        /// </summary>
        /// <param name="Text">Message</param>
        public void UdpateStatusText(string Text) {
            if (InvokeRequired) {
                // We're not in the UI thread, so we need to call BeginInvoke
                BeginInvoke(new StringParameterDelegate(UdpateStatusText), new object[] { Text });
                return;
            }
            // Must be on the UI thread if we've got this far
            label1.ForeColor = Color.Green;
            label1.Text = Text;
        }

        /// <summary>
        /// Update text in default green color of success message
        /// </summary>
        /// <param name="Text">Message</param>
        public void UdpateBodyText(string Text) {
            if (InvokeRequired) {
                // We're not in the UI thread, so we need to call BeginInvoke
                BeginInvoke(new StringParameterDelegate(UdpateBodyText), new object[] { Text });
                return;
            }
            // Must be on the UI thread if we've got this far
            label2.ForeColor = Color.Green;
            label2.Text = Text;

            //centers the starting point, not the body
            //label2.Location = new Point(pictureBox1.Width / 2, pictureBox1.Height / 2);
        }

        /// <summary>
        /// Update text in default green color of success message
        /// </summary>
        /// <param name="Text">Message</param>
        public void UdpateTitleText(string Text) {
            if (InvokeRequired) {
                // We're not in the UI thread, so we need to call BeginInvoke
                BeginInvoke(new StringParameterDelegate(UdpateTitleText), new object[] { Text });
                return;
            }
            // Must be on the UI thread if we've got this far
            label3.ForeColor = Color.Black;
            label3.Text = Text;
        }


        /// <summary>
        /// Update text with message color defined as green/yellow/red/ for success/warning/failure
        /// </summary>
        /// <param name="Text">Message</param>
        /// <param name="tom">Type of Message</param>
        public void UdpateStatusTextWithStatus(string Text, TypeOfMessage tom) {
            if (InvokeRequired) {
                // We're not in the UI thread, so we need to call BeginInvoke
                BeginInvoke(new StringParameterWithStatusDelegate(UdpateStatusTextWithStatus), new object[] { Text, tom });
                return;
            }
            // Must be on the UI thread if we've got this far
            switch (tom) {
                case TypeOfMessage.Error:
                    label1.ForeColor = Color.Red;
                    break;
                case TypeOfMessage.Warning:
                    label1.ForeColor = Color.Yellow;
                    break;
                case TypeOfMessage.Success:
                    label1.ForeColor = Color.Green;
                    break;
            }
            label1.Text = Text;

        }

        /// <summary>
        /// Prevents the closing of form other than by calling the CloseSplashScreen function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SplashForm_FormClosing(object sender, FormClosingEventArgs e) {
            if (CloseSplashScreenFlag == false)
                e.Cancel = true;
        }

    }
}
